package com.bsdsma.visonManagementSystemapp.restcontroller;

import com.bsdsma.visonManagementSystemapp.dto.SchoolDto;
import com.bsdsma.visonManagementSystemapp.dto.DataGroupByDateDto;
import com.bsdsma.visonManagementSystemapp.service.SchoolService;
import com.bsdsma.visonManagementSystemapp.utils.ResultUtils;
import com.bsdsma.visonManagementSystemapp.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * School module API endpoints
 */
@RestController
@RequestMapping(value = "/api/school")
public class SchoolRestController {

    @Autowired
    private SchoolService schoolService;

    /**
     * Search schools with pagination
     */
    @GetMapping
    public Map<String, Object> search(SchoolDto search, 
                                    @RequestParam(defaultValue = "1") Integer pageNum,
                                    @RequestParam(defaultValue = "10") Integer pageSize) {
        return ResultUtils.ok(schoolService.search(search, pageNum, pageSize));
    }

    /**
     * Get all schools
     */
    @GetMapping(value = "/list")
    public Map<String, Object> listAll() {
        return ResultUtils.ok(schoolService.listAll());
    }

    /**
     * Get school by ID
     */
    @GetMapping(value = "/{id}")
    public Map<String, Object> getById(@PathVariable Long id) {
        return ResultUtils.ok(schoolService.getById(id));
    }

    /**
     * Add new school
     */
    @PostMapping
    public Map<String, Object> add(@RequestBody SchoolDto schoolDto) {
        schoolService.add(schoolDto);
        return ResultUtils.ok();
    }

    /**
     * Update school
     */
    @PostMapping(value = "/{id}")
    public Map<String, Object> update(@PathVariable Long id, @RequestBody SchoolDto schoolDto) {
        schoolService.update(id, schoolDto);
        return ResultUtils.ok();
    }

    /**
     * Delete school
     */
    @DeleteMapping(value = "/{id}")
    public Map<String, Object> delete(@PathVariable Long id) {
        schoolService.delete(id);
        return ResultUtils.ok();
    }

    /**
     * Get total school count
     */
    @GetMapping(value = "/count")
    public Map<String, Object> count() {
        return ResultUtils.ok(schoolService.count());
    }

    /**
     * Get daily new school statistics
     */
    @GetMapping(value = "/day-newly-added")
    public Map<String, Object> dayNewlyAdded() {
        Date endDate = DateUtils.getNow();
        Date beginDate = DateUtils.dayAfter(endDate, -30);
        List<DataGroupByDateDto> dataList = schoolService.countByDay(beginDate, endDate);
        return ResultUtils.ok(dataList);
    }
} 